package bb.lanxing.device.sgdevcie.sensors

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import bb.lanxing.App
import bb.lanxing.R
import bb.lanxing.activity.bluetooth.CadenceStatusActivity
import bb.lanxing.activity.bluetooth.HeartRateBeltStatusActivity
import bb.lanxing.activity.bluetooth.HeartRateStatusActivity
import bb.lanxing.activity.bluetooth.PowerRateStatusActivity
import bb.lanxing.activity.bluetooth.BikeTrainerStatusActivity
import bb.lanxing.activity.bluetooth.Cadence01StatusActivity
import bb.lanxing.activity.bluetooth.IndoorBikeStatusActivity
import bb.lanxing.activity.bluetooth.CrossTrainerStatusActivity
import bb.lanxing.activity.bluetooth.HeartRate01StatusActivity
import bb.lanxing.activity.bluetooth.RowerStatusActivity
import bb.lanxing.activity.bluetooth.SmartAssistDeviceActivity
import bb.lanxing.activity.bluetooth.SwitchableCadenceStatusActivity
import bb.lanxing.activity.bluetooth.base.BaseSensorActivity
import bb.lanxing.activity.bluetooth.light.LightDeviceActivity
import bb.lanxing.activity.bluetooth.radar.RadarTaillightActivity
import bb.lanxing.databinding.LayoutBleSensorsListFragmentBinding
import bb.lanxing.device.sgdevcie.sensors.BleSensorListAdapter.OnSensorClickListener
import bb.lanxing.lib.devices.api.SmartDevice
import bb.lanxing.lib.devices.api.SmartDevice.TYPE_CADENCE
import bb.lanxing.lib.devices.core.utils.DeviceHelper
import bb.lanxing.lib.devices.sprint.presenter.SearchSprintPresenter
import bb.lanxing.lib.devices.sprint.view.SearchSprintView
import bb.lanxing.lib.devices.utils.DeviceUtils
import bb.lanxing.mvvm.base.BaseDBFragment
import bb.lanxing.util.Log
import kotlin.jvm.internal.Intrinsics

class BleSensorListFragment :
    BaseDBFragment<LayoutBleSensorsListFragmentBinding>(R.layout.layout_ble_sensors_list_fragment),
    SearchSprintView, OnSensorClickListener {
    private var bleSensorListAdapter: BleSensorListAdapter? = null
    private var presenter: SearchSprintPresenter? = null
    private var TAG = "BleSensorListFragment"

    override fun onStartScan() {
    }

    override fun onStopScan() {
    }

    override fun initView(binding: LayoutBleSensorsListFragmentBinding) {
        var intent: Intent
        val activity = activity
        var i = TYPE_CADENCE
        if (activity != null) {
            activity.intent?.also {
                intent = it
                i = it.getIntExtra("EXTRA_DEVICE_TYPE", TYPE_CADENCE)
            }
        }
        if (DeviceHelper.getDeviceProvider() != null) {
            this.presenter =
                SearchSprintPresenterImpl(context, DeviceHelper.getDeviceProvider(), this, i)
            val searchSprintPresenter = this.presenter
            this.bleSensorListAdapter = BleSensorListAdapter(searchSprintPresenter)
            binding.recyclerView.layoutManager = LinearLayoutManager(
                context
            )
            val recyclerView = binding.recyclerView
            val defaultItemAnimator = DefaultItemAnimator()
            defaultItemAnimator.supportsChangeAnimations = false
            recyclerView.itemAnimator = defaultItemAnimator
            binding.recyclerView.adapter = bleSensorListAdapter
//            val bleSensorListAdapter = this.bleSensorListAdapter
            bleSensorListAdapter!!.onSensorClickListener = this
            return
        }
        snack(R.string.bike_place_search_error)
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_device_search_ble, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.device_refresh_ble) {
            val searchSprintPresenter = this.presenter ?: return true
            searchSprintPresenter.clear()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onResume() {
        super.onResume()
        val searchSprintPresenter = this.presenter
        searchSprintPresenter?.startScan()
    }

    override fun onPause() {
        super.onPause()
        val searchSprintPresenter = this.presenter
        searchSprintPresenter?.stopScan()
    }

    override fun onDestroy() {
        super.onDestroy()
        val searchSprintPresenter = this.presenter
        searchSprintPresenter?.destroy()
    }

    override fun closeLoadingDialog() {
        dismissLoadingDialog()
    }

    override fun notifyDeviceUpdate(i: Int) {
        val bleSensorListAdapter = this.bleSensorListAdapter
        bleSensorListAdapter?.notifyItemChanged(i)
    }

    override fun notifyInsertedDevice() {
        val bleSensorListAdapter = this.bleSensorListAdapter
        bleSensorListAdapter?.notifyItemInserted(bleSensorListAdapter.itemCount)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun notifyDataChanged() {
        val bleSensorListAdapter = this.bleSensorListAdapter
        Log.d(TAG, "notifyDataChanged...")
        bleSensorListAdapter?.notifyDataSetChanged()
    }

    override fun getApplicationContext(): Context {
        val context = App.getContext()
        return context
    }

    override fun onClick(device: SmartDevice) {
        if (DeviceUtils.isDfuDevice(device)) {
            if (device.type == SmartDevice.TYPE_HR_X2_PRO) {
                Log.d(TAG, "onClick my deivce...")
                //todo:hu
//            UpdateDeviceIntroActivity.Companion companion = UpdateDeviceIntroActivity.Companion;
//            FragmentActivity requireActivity = requireActivity();
//            Intrinsics.checkNotNullExpressionValue(requireActivity, "requireActivity()");
//            String address = device.getAddress();
//            Intrinsics.checkNotNullExpressionValue(address, "device.address");
//            String name = device.getName();
//            Intrinsics.checkNotNullExpressionValue(name, "device.name");
//            companion.start(requireActivity, address, name);
            }
            return
        }

        if (!presenter!!.needSaveDevice(device)) {
            return
        }


        when (device.type) {
            SmartDevice.TYPE_CADENCE -> {
                val intent = Intent(context, CadenceStatusActivity::class.java)
                intent.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent)
            }
            SmartDevice.TYPE_HEARTRATE -> {
                val intent2 = Intent(context, HeartRateStatusActivity::class.java)
                intent2.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent2.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent2)
            }
            SmartDevice.TYPE_POWERRATE -> {
                val intent2 = Intent(context, PowerRateStatusActivity::class.java)
                intent2.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent2.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent2)
            }
            SmartDevice.TYPE_BIKE_TRAINER -> {
                val intent2 = Intent(context, BikeTrainerStatusActivity::class.java)
                intent2.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent2.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent2)
            }
            SmartDevice.TYPE_INDOOR_BIKE -> {
                val intent2 = Intent(context, IndoorBikeStatusActivity::class.java)
                intent2.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent2.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent2)
            }
            SmartDevice.TYPE_CROSS_TRAINER -> {
                val intent2 = Intent(context, CrossTrainerStatusActivity::class.java)
                intent2.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent2.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent2)
            }
            SmartDevice.TYPE_ROWER -> {
                val intent2 = Intent(context, RowerStatusActivity::class.java)
                intent2.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent2.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent2)
            }
            SmartDevice.TYPE_Q1 -> {
                val intent3 = Intent(context, SmartAssistDeviceActivity::class.java)
                intent3.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent3.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent3)
            }
            SmartDevice.TYPE_HEARTRATE_BELT -> {
                val intent4 = Intent(context, HeartRateBeltStatusActivity::class.java)
                intent4.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent4.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent4)
            }
            SmartDevice.TYPE_RADAR_SR -> {
                val intent5 = Intent(context, RadarTaillightActivity::class.java)
                intent5.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent5.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent5)
            }
            SmartDevice.TYPE_VORTEX_CADENCE -> {
                val intent6 = Intent(context, SwitchableCadenceStatusActivity::class.java)
                intent6.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent6.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent6)
            }
            SmartDevice.TYPE_CADENCE01 -> {
                val intent6 = Intent(context, Cadence01StatusActivity::class.java)
                intent6.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent6.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent6)
            }
            SmartDevice.TYPE_HR_X2_PRO -> {
                //TODO:hu, XossHeartRateX2ProActivity not supported.
//                val intent7 = Intent(context, XossHeartRateX2ProActivity::class.java)
                val intent7 = Intent(context, SwitchableCadenceStatusActivity::class.java)
                intent7.putExtra(BaseSensorActivity.DEVICE_ADDRESS, device.address)
                intent7.putExtra(BaseSensorActivity.DEVICE_TYPE, device.type)
                intent7.putExtra(BaseSensorActivity.DEVICE_NAME, device.name)
                startActivity(intent7);
            }
            SmartDevice.TYPE_LIGHT -> {
                val intent8 = Intent(context, LightDeviceActivity::class.java)
                intent8.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent8.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent8)
            }
            SmartDevice.TYPE_HEART_RATE01 -> {
                val intent2 = Intent(context, HeartRate01StatusActivity::class.java)
                intent2.putExtra("EXTRA_DEVICE_ADDRESS", device.address)
                intent2.putExtra("EXTRA_DEVICE_TYPE", device.type)
                startActivity(intent2)
            }
        }
        if (DeviceHelper.isConnected(device.address) || DeviceHelper.isConnecting(device.address)) {
            return
        }
        DeviceHelper.connect(device)
    }

    companion object {
        fun newInstance(): BleSensorListFragment {
            return BleSensorListFragment()
        }
    }
}
